WWW/UnityWebRequestPOST/GET请求不会从server/url返回最新数据

您所在的位置:网站首页 apt get 搜索 WWW/UnityWebRequestPOST/GET请求不会从server/url返回最新数据

WWW/UnityWebRequestPOST/GET请求不会从server/url返回最新数据

2023-03-09 10:37| 来源: 网络整理| 查看: 265

1> Programmer..:

发生这种情况是因为在服务器上启用了资源缓存.

我知道的三种可能的解决方案:

1.Disable资源缓存在服务器上.每个Web服务器的说明都不同.通常在.htaccess.

2.使用唯一的时间戳生成每个请求.时间应该是Unix格式.

此方法不适用于iOS.你很好,因为这是为了HoloLens.

例如,如果您的网址是http://url.com/file.rar,则追加?t=currentTime到最后.currentTime是Unix格式的实际时间.

完整示例网址: http://url.com/file.rar?t=1468475141

代码:

string getUTCTime() { System.Int32 unixTimestamp = (System.Int32)(System.DateTime.UtcNow.Subtract(new System.DateTime(1970, 1, 1))).TotalSeconds; return unixTimestamp.ToString(); } private IEnumerator WaitForRequest() { string url = "API Link goes Here" + "?t=" + getUTCTime(); WWW get = new WWW(url); yield return get; getreq = get.text; //check for errors if (get.error == null) { string json = @getreq; List data = JsonConvert.DeserializeObject>(json); int l = data.Count; text.text = "Data: " + data[l - 1].content; } else { Debug.Log("Error!-> " + get.error); } }

3.通过提供和修改请求中的和头来Disable缓存客户端.Cache-ControlPragma

设置Cache-Control 标题来max-age=0, no-cache, no-store然后设置Pragma 标题 来no-cache.

我建议你这样做UnityWebRequest而不是WWW上课.首先,包括using UnityEngine.Networking;.

代码:

IEnumerator WaitForRequest(string url) { UnityWebRequest www = UnityWebRequest.Get(url); www.SetRequestHeader("Cache-Control", "max-age=0, no-cache, no-store"); www.SetRequestHeader("Pragma", "no-cache"); yield return www.Send(); if (www.isError) { Debug.Log(www.error); } else { Debug.Log("Received " + www.downloadHandler.text); } }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3